<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//
namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;
use AuctionCore\Common\Util;
use AuctionCore\System\AuctionException;

/**
 * 开奖活动管理
 *
 * @author Xmh
 */
class LottoryController extends BaseController
{
    private $lottoryLogic;

    protected $lottoryTicketLogic;

    private $empId;

    public function __construct()
    {
        parent::__construct();
        $this->empId = $this->user_info['emp_id'];

        $this->lottoryLogic = D('Lottory', 'Logic');

        $this->lottoryTicketLogic = D('LottoryTicket', 'Logic');
        //模板赋值
        $this->assign("emp_id", $this->empId);

    }

    public function index()
    {
        $this->display();
    }

    /**
     * 开奖活动列表
     */
    public function lottoryList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = [];
        $params['lot_id'] = I('get.lot_id');
        $params['lot_name'] = I('get.lot_name');
        $params['lot_type'] = I('get.lot_type');
        $params['lot_status'] = I('get.lot_status');
        $params['start_time'] = I('get.start_time');
        $params['end_time'] = I('get.end_time');
        $result = $this->lottoryLogic->getLottoryList($page, $size, $params);
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();   // 分页显示输出

        foreach ($result['list'] as &$v) {
            $v['lottory_start_time'] = date('Y-m-d H:i:s', $v['lottory_start_time']);
            $v['lottory_end_time'] = date('Y-m-d H:i:s', $v['lottory_end_time']);
            $v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        }
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }

    /**
     * 创建活动页面
     */
    public function lottoryCreate()
    {
        $this->display();
    }

    /**
     * 执行创建
     */
    public function createLottory()
    {
        $lottoryName = I('post.lottory_name', '', 'trim');
        if (empty($lottoryName)) {
            $this->helpjsonresult('400', '抱歉，活动名称不能为空，请填写活动名称');
        }
        if (strlen($lottoryName) > 150) {
            $this->helpjsonresult('400', '抱歉，活动名称在50个汉字以内');
        }
        $lottoryImage = I('post.lottory_image');
        if (empty($lottoryImage)) {
            $this->helpjsonresult('400', '抱歉，活动图片不能为空，请上传活动图片');
        }
        $lottoryType = I('post.lottory_type');
        $lottoryAward = I('post.lottory_award', '', 'trim');
        if (empty($lottoryAward)) {
            $this->helpjsonresult('400', '抱歉，活动奖品不能为空，请填写活动奖品');
        }
        if (strlen($lottoryAward) > 60) {
            $this->helpjsonresult('400', '抱歉，奖品名称在20个汉字以内');
        }
        $lottoryStartTime = I('post.lottory_start_time');
        if (empty($lottoryStartTime)) {
            $this->helpjsonresult('400', '抱歉，活动开始时间不能为空，请选择开始时间');
        }
        $now = date("Y-m-d H:i:s");
        if ($lottoryStartTime < $now) {
            $this->helpjsonresult('400', '抱歉，活动开始时间不能小于当前时间');
        }
        $lottoryEndTime = I('post.lottory_end_time');
        if (empty($lottoryEndTime)) {
            $this->helpjsonresult('400', '抱歉，活动结束时间不能为空，请选择结束时间');
        }
        if ($lottoryEndTime < $lottoryStartTime) {
            $this->helpjsonresult('400', '抱歉，活动结束时间不能小于开始时间');
        }
        $lottoryTime = I('post.lottory_time');
        if (empty($lottoryTime)) {
            $this->helpjsonresult('400', '抱歉，活动开奖时间不能为空，请选择开奖时间');
        }
        if ($lottoryTime < $lottoryEndTime) {
            $this->helpjsonresult('400', '抱歉，活动开奖时间不能小于结束时间');
        }
        $data['lottory_name'] = $lottoryName;
        $data['lottory_image'] = $lottoryImage;
        $data['lottory_type'] = $lottoryType;
        $data['lottory_award'] = $lottoryAward;
        $data['lottory_start_time'] = strtotime($lottoryStartTime);
        $data['lottory_end_time'] = strtotime($lottoryEndTime);
        $data['lottory_time'] = strtotime($lottoryTime);
        $data['create_time'] = NOW_TIME;
        $res = $this->lottoryLogic->createLottory($data);
        if ($res === false) {
            $this->helpjsonresult('500', '新建活动失败');
        }
        $this->helpjsonresult('200', '新建活动成功');
    }

    /**
     * 编辑开奖活动详情
     */
    public function lottoryEdit()
    {
        $lotId = I('get.id/d');
        if ($lotId < 1) {
            header("Location:" . U('Lottory/index'));
        }
        $lottoryDetail = $this->lottoryLogic->getLottoryDetail($lotId);
        $this->assign('lottory', $lottoryDetail);
        $this->display();
    }

    /**
     * 获得开奖活动详情
     * return array 活动详情
     */
    public function getLottoryDetail()
    {
        $lotId = I('request.lot_id/d', 0);
        if ($lotId < 1) {
            $this->helpJsRedirect('活动ID错误');
        }
        $lottoryDetail = $this->lottoryLogic->getLottoryDetail($lotId);
        $lottoryDetail['lottory_ticket_nums'] = $this->lottoryTicketLogic->getLottoryTicketNum($lotId);
        echo json_encode($lottoryDetail);
    }

    /**
     * 获取中奖号码
     */
    public function getLottoryWinNum()
    {
        $jq_num = I('post.lottory_ticket_nums/d');          //奖券数量
        $sse = I('post.sse');                                 //上证指数
        $szs = I('post.szs');                                 //深证指数
        $lottoryWinNum = get_win_num($sse, $szs, $jq_num);
        $lottory['win_num'] = $lottoryWinNum;
        echo json_encode($lottory);
    }

    /**
     * 开奖操作
     */
    public function runLottory()
    {
        $lotId = I('post.lottory_id/d');
        $lottoryWinNum = I('post.lottory_win_num');
        $sse = I('post.sse');
        $szs = I('post.szs');
        $res = $this->lottoryLogic->doRunLottory($lotId, $sse, $szs, $lottoryWinNum);
        if (!$res) {
            $this->helpJsonResult('500', '开奖失败');
        }
        $this->helpJsonResult('200', '开奖成功');
    }


    /**
     * 上线、下线操作
     */
    public function changeLottoryStatus()
    {
        $lotId = I('post.lot_id/d');
        $lotStatus = I('post.lot_status/d');
        $res = $this->lottoryLogic->updateLottoryStatus($lotId, $lotStatus);
        if ($res) {
            echo Util::auto_jsonp_or_json($_REQUEST, ['code' => 200, 'msg' => '上线操作成功']);
        } else {
            echo Util::auto_jsonp_or_json($_REQUEST, ['code' => 500, 'msg' => '上线操作失败']);
        }
    }

    /**
     * 查看开奖活动详情
     */
    public function lottoryCheck()
    {
        $lotId = I('get.id/d');
        if ($lotId < 1) {
            header("Location:" . U('Lottory/index'));
        }
        $lottoryDetail = $this->lottoryLogic->getLottoryDetail($lotId);
        $lottoryTicketNum = $this->lottoryTicketLogic->getLottoryTicketNum($lotId);
        $this->assign('lottory', $lottoryDetail);
        $this->assign('lottory_ticket_num', $lottoryTicketNum);
        $this->display();
    }

    /**
     * 上传开奖活动图片
     */
    public function uploadPic()
    {
        try {
            $ret = [];
            foreach ($_FILES as $key => $file) {
                $tmp_file = $file['tmp_name'];
                $object = $tmp_file . '.' . get_extension($file['name']);
                $ret[] = \AuctionCore\Common\Image::upload_file_by_file(OSS_BUCKET, $object, $tmp_file, null, '', 'lottory');
            }
            echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '200', 'msg' => '', 'data' => $ret]);
            exit;
        } catch (\Exception $e) {
            echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '5', 'msg' => $e->getMessage()]);
            exit;
        }
    }

    /**
     * 更新图片信息到数据库
     */
    public function uploadPicInfo()
    {
        try {
            $lottoryImage = $_REQUEST['lottory_image'];
            if (!$lottoryImage)
                AuctionException::make_throw('200009');
            $lottory['id'] = I("post.lot_id/d");
            $lottory['lottory_image'] = $lottoryImage;
            $this->lottoryLogic->saveLottory($lottory);
            echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '200', 'msg' => '']);
        } catch (AuctionException $e) {
            echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '5', 'msg' => $e->getMessage()]);
            exit;
        }
    }

    /**
     * 更新编辑活动信息
     */
    public function updateLottory()
    {
        $id = I('post.lot_id/d');
        $lottoryName = I('post.lottory_name', '', 'trim');
        if (empty($lottoryName)) {
            $this->helpjsonresult('400', '抱歉，活动名称不能为空，请填写活动名称');
        }
        if (strlen($lottoryName) > 150) {
            $this->helpjsonresult('400', '抱歉，活动名称在50个汉字以内');
        }
//        $lottoryImage = I('post.lottory_image');
//        if(empty($lottoryImage)){
//            $this->helpjsonresult('400', '抱歉，活动图片不能为空，请上传活动图片');
//        }
        $lottoryType = I('post.lottory_type');
        $lottoryAward = I('post.lottory_award', '', 'trim');
        if (empty($lottoryAward)) {
            $this->helpjsonresult('400', '抱歉，活动奖品不能为空，请填写活动奖品');
        }
        if (strlen($lottoryAward) > 60) {
            $this->helpjsonresult('400', '抱歉，奖品名称在20个汉字以内');
        }
        $lottoryStartTime = strtotime(I('post.lottory_start_time'));
        if (empty($lottoryStartTime)) {
            $this->helpjsonresult('400', '抱歉，活动开始时间不能为空，请选择开始时间');
        }
        $now = date("Y-m-d H:i:s");
        if ($lottoryStartTime < $now) {
            $this->helpjsonresult('400', '抱歉，活动开始时间不能小于当前时间');
        }
        $lottoryEndTime = strtotime(I('post.lottory_end_time'));
        if (empty($lottoryEndTime)) {
            $this->helpjsonresult('400', '抱歉，活动结束时间不能为空，请选择结束时间');
        }
        if ($lottoryEndTime < $lottoryStartTime) {
            $this->helpjsonresult('400', '抱歉，活动结束时间不能小于开始时间');
        }
        $lottoryTime = strtotime(I('post.lottory_time'));
        if (empty($lottoryTime)) {
            $this->helpjsonresult('400', '抱歉，活动开奖时间不能为空，请选择开奖时间');
        }
        if ($lottoryTime < $lottoryEndTime) {
            $this->helpjsonresult('400', '抱歉，活动开奖时间不能小于结束时间');
        }
        $data['id'] = $id;
        $data['lottory_name'] = $lottoryName;
//        $data['lottory_image'] = $lottoryImage;
        $data['lottory_type'] = $lottoryType;
        $data['lottory_award'] = $lottoryAward;
        $data['lottory_start_time'] = $lottoryStartTime;
        $data['lottory_end_time'] = $lottoryEndTime;
        $data['lottory_time'] = $lottoryTime;
        $res = $this->lottoryLogic->saveLottory($data);
        if ($res === false) {
            $this->helpjsonresult('500', '更新活动失败');
        }
        $this->helpjsonresult('200', '更新活动成功');
    }


    /**
     * 开奖活动列表页
     */
    public function lottoryTicket()
    {
        $lotId = I('get.id/d');
        if ($lotId < 1) {
            header("Location:" . U('Lottory/index'));
        }
        $lottoryDetail = $this->lottoryLogic->getLottoryDetail($lotId);
        $this->assign('lottory', $lottoryDetail);
        $this->assign('lot_id', $lotId);
        $this->display();
    }

    /**
     * 开奖活动对应抽奖券列表
     */
    public function lottoryTicketList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = [];
        $params['lot_id'] = I('get.lot_id');  //活动id
        $params['lt_num'] = I('get.lt_num');  //奖券号码
        $params['lt_type'] = I('get.lt_type');
        $params['lt_uid'] = I('get.lt_uid');
        $params['lt_phone'] = I('get.lt_phone');
        $params['invite_uid'] = I('get.invite_uid');
        $params['lt_status'] = I('get.lt_status');
        $params['start_time'] = I('get.start_time');
        $params['end_time'] = I('get.end_time');
        $result = $this->lottoryTicketLogic->getLottoryTicketList($page, $size, $params);
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();          // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }

}
